// index.js
// 获取应用实例
const app = getApp()
const QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js')
var qqmapsdk
var template = require('../../template/template.js');


Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
     banner:[],
    duration: 2000,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    loading: false, 
    imgUrls: [
      '../../image/swipe1.jpg',
      '../../image/swipe2.jpg',
      '../../image/swipe3.jpg'
    ],  
    real_address:"请允许获取您的位置信息",
    longitude:119.587792,   //经度
    latitude:32.319999    //维度
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    let that = this
    template.tabbar("tabBar", 0, this)//0表示第一个tabbar,根据不同的页面修改序号

    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }


    if(wx.getStorageSync('city')!=""&&wx.getStorageSync('city')!=null&&wx.getStorageSync('city')!=undefined){
      //需要获取位置
      that.setData({
        real_address:wx.getStorageSync('city')
      })            
    }

    qqmapsdk = new QQMapWX({
      key: 'BYXBZ-763KX-YTT4S-7SEOC-N5I23-I5BAE'
    })

    //获取用户所在城市
    that.getUserLocation()
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  gotoMap:function(){
    let that = this
   
    wx.openLocation({ 
      latitude: that.data.latitude,      
      longitude:that.data.longitude,      
      name: "李典社区党群服务中心",      
      address: '江苏省扬州市广陵区X305江洲小区东南侧',      
      scale: 20      
    })
  },
  tab1click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=1'
    })
    
  },
  tab2click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=2'
    })    
  },
  tab3click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=3'
    })
    
  },
  tab4click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=4'
    })
    
  },
  tab5click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=5'
    })
    
  },
  tab6click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=6'
    })
    
  },
  tab7click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=7'
    })
    
  },

  tab8click(){
    let that = this
    wx.navigateTo({
      url: '../advantageshow/advantageshow?tab=8'
    })
  },
  checkhouse(){
    let that = this
    wx.navigateTo({
      url: '../checkhouse/checkhouse',
    
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })

  },
  recycle(){
     let that = this
    wx.navigateTo({
      url: '../recycle/recycle',
     
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })

  },
  economy(){
    let that = this
    wx.navigateTo({
      url: '../economy/economy',
     
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })

  },
  change(){
     let that = this
      wx.navigateTo({
        url: '../change/change',
       
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
  },

  allowLocation(){
    let that = this
    if(that.data.real_address == "请允许获取您的位置信息"){
       
        let reportContent = {lat:"",lon:"",lonlat:""}
        wx.getSetting({
            success: (res) => {
                let authStatus = res.authSetting['scope.userLocation'];
                console.log("authStatus="+authStatus)
                // 如果没有授权位置
                if(!authStatus){
                    //向用户发起授权请求                   
                    wx.authorize({
                        scope: "scope.userLocation",
                        success: (res) => {
                            // console.log(res)
                            wx.getLocation({
                                type: 'wgs84',
                                geocode:true,//设置该参数为true可直接获取经纬度及城市信息
                                success: function (res) {                                    
                                    reportContent.lat = res.latitude
                                    let latitude = res.latitude
                                    reportContent.lon = res.longitude
                                    let longitude = res.longitude
                                    reportContent.lonlat = '[' + res.longitude + ',' + res.latitude + ']'
                                    qqmapsdk.reverseGeocoder({//SDK调用
                                      location: { latitude, longitude },
                                      success: function (res) {
                                        var cur_city =  res.result.address_component.city

                                        wx.setStorageSync('city', cur_city)
                                        console.log("cur_city="+cur_city)
                                        that.setData({
                                          real_address:cur_city
                                        })
                                      },
                                      fail:function(err){
                                        console.log(err, "getCity error");
                                      }
                                    })
                                },
                                fail: function () {
                                  wx.showToast({
                                        title: '获取地址失败，请打开手机的定位服务',
                                        icon:'none',
                                        duration: 1500
                                    });
                                }
                            });
                        },
                        fail: (err) => {
                            console.log(err)
                            wx.showModal({
                                title: '授权失败',
                                content: '需要获取您当前位置，请在设置界面打开相关权限',
                                success: (res) => {
                                    console.log(res)
                                    if (res.confirm) {
                                        wx.openSetting()
                                    }
                                }
                            })
                        }
                    })
                }else{
                    console.log('已授权')
                    wx.getLocation({
                        type: 'wgs84',
                        geocode:true,//设置该参数为true可直接获取经纬度及城市信息
                        success: function (res) {
                            console.log(res)
                            reportContent.lat = res.latitude
                            let latitude =  res.latitude
                            reportContent.lon = res.longitude
                            let longitude = res.longitude
                            reportContent.lonlat = '[' + res.longitude + ',' + res.latitude + ']'
                            qqmapsdk.reverseGeocoder({//SDK调用
                              location: { latitude, longitude },
                              success: function (res) {
                                var cur_city =  res.result.address_component.city
                                 wx.setStorageSync('city', cur_city)
    
                             
                                that.setData({
                                    real_address:cur_city
                                })
                              },
                              fail:function(err){
                                console.log(err, "getCity error");
                              }
                            })
                        },
                        fail: function () {
                            wx.showToast({
                                title: '获取地址失败，请打开手机的定位服务',
                                icon:'none'
                            });
                        }
                    });
                }
            }
        })    
    }

  },
  // 获取当前用户的位置
  getUserLocation(){
    let that = this
    let reportContent = {lat:"",lon:"",lonlat:""}
    wx.getSetting({
        success: (res) => {
            let authStatus = res.authSetting['scope.userLocation'];
            console.log("authStatus="+authStatus)
            // 如果没有授权位置
            if(!authStatus){
                //向用户发起授权请求
                console.log('授权111')
                wx.authorize({
                    scope: "scope.userLocation",
                    success: (res) => {
                        // console.log(res)
                        wx.getLocation({
                            type: 'wgs84',
                            geocode:true,//设置该参数为true可直接获取经纬度及城市信息
                            success: function (res) {
                              console.log("allow ****************")
                                console.log(res)
                                reportContent.lat = res.latitude
                                let latitude = res.latitude
                                reportContent.lon = res.longitude
                                let longitude = res.longitude
                                reportContent.lonlat = '[' + res.longitude + ',' + res.latitude + ']'
                                qqmapsdk.reverseGeocoder({//SDK调用
                                  location: { latitude, longitude },
                                  success: function (res) {
                                    var cur_city =  res.result.address_component.city
                                    console.log("cur_city="+cur_city)
                                     wx.setStorageSync('city', cur_city)
                                    that.setData({
                                      real_address:cur_city
                                    })
                                  },
                                  fail:function(err){
                                    console.log(err, "getCity error");
                                  }
                                })
                            },
                            fail: function () {
                              wx.showToast({
                                    title: '获取地址失败，请打开手机的定位服务',
                                    icon:'none',
                                    duration: 1500
                                });
                            }
                        });
                    },
                    fail: (err) => {
                        console.log(err)
                        wx.showModal({
                            title: '授权失败',
                            content: '需要获取您当前位置，请在设置界面打开相关权限',
                            success: (res) => {
                                console.log(res)
                                if (res.confirm) {
                                    // wx.openSetting({
                                    //     success (res) {
                                    //         console.log(res.authSetting)
                                    //         // res.authSetting = {
                                    //         //   "scope.userInfo": true,
                                    //         //   "scope.userLocation": true
                                    //         // }
                                    //     }
                                      
                                    // })
                                }
                            }
                        })
                    }
                })
            }else{
                console.log('已授权')
                wx.getLocation({
                    type: 'wgs84',
                    geocode:true,//设置该参数为true可直接获取经纬度及城市信息
                    success: function (res) {
                        console.log(res)
                        reportContent.lat = res.latitude
                        let latitude =  res.latitude
                        reportContent.lon = res.longitude
                        let longitude = res.longitude
                        reportContent.lonlat = '[' + res.longitude + ',' + res.latitude + ']'
                        qqmapsdk.reverseGeocoder({//SDK调用
                          location: { latitude, longitude },
                          success: function (res) {
                            var cur_city =  res.result.address_component.city
                             wx.setStorageSync('city', cur_city)

                            console.log("cur_city="+cur_city)
                            that.setData({
                                real_address:cur_city
                            })
                          },
                          fail:function(err){
                            console.log(err, "getCity error");
                          }
                        })
                    },
                    fail: function () {
                        wx.showToast({
                            title: '获取地址失败，请打开手机的定位服务',
                            icon:'none'
                        });
                    }
                });
            }
        }
    })    
}
})

